<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
A larger example
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial067.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial063.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial069.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc139">9.5</A>&nbsp;&nbsp;A larger example</H2>
<A NAME="farm-example"></A>
Consider the following problem:
<BLOCKQUOTE CLASS="quote">
George is contemplating buying a farm which is a very strange shape,
comprising a large triangular lake with a square field on each side. The
area of the lake is exactly seven acres, and the area of each field is an
exact whole number of acres. Given that information, what is the smallest
possible total area of the three fields?
</BLOCKQUOTE>
A diagram of the farm is shown in Figure&nbsp;<A HREF="#lake-fields">9.6</A>.
<BLOCKQUOTE CLASS="figure"><DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV>
<DIV CLASS="center">
<IMG SRC="tutorial030.gif">
</DIV>
<BR>
<BR>
<DIV CLASS="center">Figure 9.6: Triangular lake with adjoining square fields</DIV><BR>
<BR>

<A NAME="lake-fields"></A>
<DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>
This is a problem which mixes both integer and real quantities, and as such
is ideal for solving with the IC library. A model for the problem appears
below. The <TT>farm/4</TT> predicate sets up the constraints between the
total area of the farm <TT>F</TT> and the lengths of the three sides of the
lake, <TT>A</TT>, <TT>B</TT> and <TT>C</TT>.<BR>
<BR>

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
:- lib(ic).

farm(F, A, B, C) :-
        [A, B, C] :: 0.0 .. 1.0Inf,     % The 3 sides of the lake
        triangle_area(A, B, C, 7),      % The lake area is 7

        [F, FA, FB, FC] :: 1 .. 1.0Inf, % The square areas are integral
        square_area(A, FA),
        square_area(B, FB),
        square_area(C, FC),
        F #= FA+FB+FC,

        FA $&gt;= FB, FB $&gt;= FC.           % Avoid symmetric solutions

triangle_area(A, B, C, Area) :-
        S $&gt;= 0,
        S $= (A+B+C)/2,
        Area $= sqrt(S*(S-A)*(S-B)*(S-C)).

square_area(A, Area) :-
        Area $= sqr(A).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE><BR>
A solution to the problem can then be found by first instantiating the area
of the farm, and then using <A HREF="../bips/lib/ic/locate-2.html"><B>locate/2</B></A><A NAME="@default249"></A>
to find the lengths of the sides of the lakes. Instantiating the area of
the farm first ensures that the first solution returned will be the minimal
one, since <A HREF="../bips/lib/ic/indomain-2.html"><B>indomain/1</B></A><A NAME="@default250"></A> always
chooses the smallest possible value first:<BR>
<BR>

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
solve(F) :-
        farm(F, A, B, C),               % the model
        indomain(F),                    % ensure that solution is minimal
        locate([A, B, C], 0.01).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE><BR>
<HR>
<A HREF="tutorial067.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial063.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial069.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
